package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author 苏成博
 * @date 2023-02-11 14:41
 */
@RestController
@Slf4j
@Api(tags = "用户请求")
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        User user = userService.userLogin(userLoginDTO);
        //生成token
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), hashMap);
        //封装UserLoginVO对象
        UserLoginVO userLoginVO=UserLoginVO.builder()
                .openid(user.getOpenid())
                .id(user.getId())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }
}
